$(function(){
//随手拍详情评论数
	$countTxt=$('.comment-count');
	/*var commentNum=$countTxt.parents('.comment-list-wrapper').find('.comment-h1').length;
	$countTxt.text(commentNum);*/
	//回复评论
	function commentChoose(event){
		event.stopPropagation();
		$_this=$(this);
		$_this.parents('.comment-list-wrapper').find('.comment-h1').removeClass('comment-target');
		$_this.parents('.comment-list-wrapper').find('.comment-h2').removeClass('comment-target');
		$_this.addClass('comment-target');
		$('input[name=content]').val('').attr('placeholder','');
		var user = "@";
		var parent_id = 0;
		var touserid = 0;
		var med = 'comment';
		switch(event.data.type){
			case 1:
				user += $_this.find('p.comment-user').attr('data-user');
				parent_id = $_this.find('p.comment-user').attr('data-id');
				touserid = $_this.find('p.comment-user').attr('data-uid');
				med = 'replymsg';
				break;
			case 2:
				user += $_this.find('span.comment-user').attr('data-user');
				parent_id = $_this.find('span.comment-user').attr('data-id');
				touserid = $_this.find('span.comment-user').attr('data-uid');
				med = 'replymsg';
				break;
			default:
				user = '我要写评论';
				parent_id = 0;
				touserid = 0;
				//user+='username';
		}
		//$('.send-comment-footer input').attr('placeholder',user);
		
		$('input[name=content]').attr('placeholder',user);
		$('input[name=parent_id]').val(parent_id);
		$('input[name=touserid]').val(touserid);
		$('input[name=med]').val(med);
	}
	
	//$('.comment-h1').on('click',{type:1},commentChoose);
	$('#thelist').on('click', '.comment-h1',{type:1},commentChoose);
	//$('.comment-h2').on('click',{type:2},commentChoose);
	$('#thelist').on('click', '.comment-h2',{type:2},commentChoose);
	$('.ssp-detail-wrapper').on('click',{type:3},commentChoose);

	$('.send-comment-submit').click(function(event) {
		event.preventDefault();
		var _this = $(this);
		var type = _this.attr('data-type');
		if(type == 'yes')
		{
			var content = $('input[name=content]').val();
			if(content == undefined || content == '')
			{
				showToast('评论内容不能为空');
				return false;
			}
			if(content.length < 3)
			{
				showToast('评论的内容不能少于3个字');
				return false;
			}
			var infoid = $('input[name=infoid]').val();
			var typeid = $('input[name=type]').val();
			var parent_id = $('input[name=parent_id]').val();
			var touserid = $('input[name=touserid]').val();
			var med = $('input[name=med]').val();
			_this.attr('data-type', 'no');
			$.ajax({
				url: '/weixin/index/'+med,
				type: 'POST',
				dataType: 'json',
				data: {
					content: content,
					infoid: infoid,
					type: typeid,
					parent_id: parent_id,
					touserid: touserid
				},
				success: function(res){
					if(res.status == 1)
					{
						showToast('评论成功');
						var temp = res.info;
						if($('.comment-target').hasClass('comment-h1'))
						{
							var str='<div class="comment-h2 clickable"><span class="cl-f-lorange comment-user" data-user="'+ temp.touserName +'" data-id="'+ temp.id +'" data-uid="'+ temp.touserid +'">'+'@'+temp.touserName;
								str+='</span>';
								str+=temp.content;
								str+='</div>';
								$('.comment-target').find('.comment-content').append(str);
						}
						else if($('.comment-target').hasClass('comment-h2'))
						{
//							var str='<div class="comment-h2 clickable"><span class="cl-f-lorange comment-user" data-user="'+ temp.nickname +'" data-id="'+ temp.id +'" data-uid="'+ temp.userid +'">'+ temp.nickname +'@'+temp.touserName
							var str='<div class="comment-h2 clickable"><span class="cl-f-lorange comment-user" data-user="'+ temp.touserName +'" data-id="'+ temp.id +'" data-uid="'+ temp.touserid +'">'+'@'+temp.touserName;
							str+='</span>';
							str+=temp.content;
							str+='</div>';
								$('.comment-target').parent().children(':last').after(str);
						}
						else
						{
							var str='<li class="ub comment-h1 clickable row" id="msg_"'+ temp.id +'><img src="'+ temp.uavatar +'" class="tx-cir lazy">';
							str+='<div class="comment-text ub-f1"><p class="cl-f-lorange comment-user" data-user="'+ temp.nickname +'" data-id="'+ temp.id +'" data-uid="'+ temp.userid +'">';
							str+=temp.nickname;
							str+='</p><p class="cl-f-lgrey comment-time">'+ temp.create_time +'</p>';
							str+='<div class="cl-f-grey comment-content"><p>';
							str+=temp.content;
							str+='</p></div></div></li>';
							$('.comment-list-title').after(str);
						}
						location = '#msg_'+ temp.id;
						$('input[name=content]').attr('placeholder','我要写评论').val('');
						$('input[name=parent_id]').val(0);
						$('input[name=touserid]').val(0);
						$('input[name=med]').val('comment');
						var text = $countTxt.text();
						$countTxt.text(parseInt(text) + 1);
					}
					else
					{
						showToast(res.info);
					}
					_this.attr('data-type', 'yes');
				},
				error: function(res){
					showToast('系统繁忙');
				}
			});
		}
		else
		{
			showToast('提交评论中');
		}
	});
	
	$('body').on('click', '.comment-js', function(e){
		e.preventDefault();
		var url = $(this).attr('data-url');
		if(url == undefined || url == '')
		{
			return false;
		}
		location.href = url;
	});
});